<h1>Izlog</h1>

<?= $this->Form->submit('Sačuvaj novi redosled', array(
    'type'  => 'button',
    'class' => 'right savePositions',
    'onclick' => 'savePositions()'
)) ?>
<p><?= $this->Html->link('+ Nova stavka', '/admin_slideshow/create') ?></p>

<br clear="all">
<table class="sortable">
<thead>
<tr>
    <th width="70%">Naslov</th>
    <th>Slika</th>
    <th></th>
</tr>
</thead>

<tbody>
<? foreach ($slideshow as $slide): ?>
<? $s = $slide['Slideshow'] ?>
<tr id="<?= $s['id'] ?>">
    <td><?= $this->Html->link($s['title'], '/admin_slideshow/edit/'.$s['id']) ?></td>
    <td><?= $this->Html->image('slideshow/'.$s['image'].'?w=100') ?></td>
    <td><?= $this->Html->link('obriši', '/admin_slideshow/remove/'.$s['id'], array(
        'confirm' => 'Da li želite da obrišete stavku iz izloga?'
    )) ?></td>
</tr>
<? endforeach ?>
</tbody>
</table>

<script type="text/javascript">
$(function(){
	$('table').sortable({
		items: 'tbody tr',
		placeholder: 'placeholder',
		stop: function(e, ui) {
			$('.savePositions').show();
		}
	});
});
function savePositions() {
	var positions = [];
	$('tbody tr').each(function(index) {
		positions.push({
			Slideshow: {
    			id:       this.id,
    			position: index
			}
		});
	});

	$.ajax({
		url: '/admin_slideshow/save_positions',
		type: 'post',
		data: {positions: positions},
		beforeSend: function() {
			$('table').addClass('loading');
		},
		complete: function(req) {
			$('table').removeClass('loading');
			if (req.status != 200) {
				alert('Greška pri čuvanju redolseda.');
			} else {
				$('.savePositions').hide();
			}
		}
	});
}
</script>